{{- define "main" }}
{{- $scratch := newScratch }}
{{- if isset .Params "image" }}
  {{- $scratch.Set "image" .Params.image }}
{{- else }}
  {{ $scratch.Set "image" .Site.Params.fallBackOgImage }}
{{- end }}
{{- $image := $scratch.Get "image" }}
{{- $bg := (path.Join "images" $image | absLangURL) }}

<!-- toc相关代码 -->
{{ $headers := findRE "<h[1-4].*?>(.|\n])+?</h[1-4]>" .Content }}
{{ $h1_n := len (findRE "(.|\n])+?" .Content) }}
{{ $re := (cond (eq $h1_n 0) "<h[2-4]" "<h[1-4]") }}
{{ $renum := (cond (eq $h1_n 0) "[2-4]" "[1-4]") }}



<div class="grid-inverse wrap content">
  <article class="post_content">
    {{- partial "i18nlist" . }}
    <h1 class="post_title">{{ .Title }}</h1>
    {{- partial "post-meta" . }}
    约{{ .WordCount }}字
    {{ with .Params.featureImage }}
    <img src="{{ . }}" class="image_featured">
    {{ end }}

    {{- .Content }}
    {{ if and ( ne .Site.Params.comment false ) ( ne .Params.comment false ) }}
      {{ partial "comments" . }}
    {{ end }}
  </article>

<!--Scrollspy-->
<div class="toc">

    <div class="page-header"><strong>- 目录 -</strong></div>

    <div id="page-scrollspy" class="toc-nav">

        {{ range $headers }}
        {{ $header := . }}
            {{ range first 1 (findRE $re $header 1) }}
                {{ range findRE $renum . 1 }}
                {{ $next_heading := (cond (eq $h1_n 0) (sub (int .) 1 ) (int . ) ) }}
                    {{ range seq $next_heading }}
                    <ul class="nav">
                    {{end}}
                    {{ $anchorId := (replaceRE ".* id=\"(.*?)\".*" "$1" $header ) }}
                        <li class="nav-item">
                            <a class="nav-link text-left" href="#{{ $anchorId }}">
                            {{ $header | plainify | htmlUnescape }}
                            </a>
                        </li>
                    <!-- close list -->
                    {{ range seq $next_heading }}
                    </ul>
                    {{ end }}
                {{ end }}
            {{ end }}
        {{ end }}

    </div>

</div>
<!--Scrollspy-->


</div>
{{- end }}